package com.hellpro.lolbox.factory;

import android.support.v4.app.Fragment;
import android.util.SparseArray;

import com.hellpro.lolbox.home.weight.HomeFragment;
import com.hellpro.lolbox.middle.MiddleFragment;
import com.hellpro.lolbox.mine.MineFragment;
import com.hellpro.lolbox.moment.view.MomentFragment;
import com.hellpro.lolbox.video.view.VideoFragment;

/**
 * 类描述:
 * 创建人:阿犇
 * 创建时间:2016/10/18
 * 备注:
 */

public class MainFragmentFactory {
    private static final int FRAGMENT_HOME = 0;
    private static final int FRAGMENT_VIDEO = 1;
    private static final int FRAGMENT_MIDDLE = 2;
    private static final int FRAGMENT_MOMENT = 3;
    private static final int FRAGMENT_MINE = 4;


    //SparseArray:key是整形的key,根据整形的key来获取一个对象,效率比较高.
    public static SparseArray<Fragment> fragments = new SparseArray<>();

    //创建一个工厂方法,用来创建一个Fragment对象
    public static Fragment createFragment(int index) {
        //从集合中获取
        Fragment fragment = fragments.get(index);
        if (fragment == null) {
            switch (index) {
                case FRAGMENT_HOME:
                    fragment = new HomeFragment();
                    break;
                case FRAGMENT_VIDEO:
                    fragment = new VideoFragment();
                    break;
                case FRAGMENT_MIDDLE:
                    fragment = new MiddleFragment();
                    break;
                case FRAGMENT_MOMENT:
                    fragment = new MomentFragment();
                    break;
                case FRAGMENT_MINE:
                    fragment = new MineFragment();
                    break;
            }
            //存到集合中
            fragments.put(index, fragment);
        }

        return fragment;
    }

}
